package hkust.lingx.csit5110.android.lib.client;

import hkust.lingx.csit5110.android.lib.model.AnchorMessage;

import java.io.ObjectOutputStream;
import java.net.Socket;

/**
 * use the client to send message to Server(another android phone)
 * @author lingx825
 *
 */
public class FeedBackClient {
  private Socket socket;

  /**
   * set up connection to server through ip and port
   * @param ip
   * @param port
   * @throws Exception
   */
  public void setUpConn(String ip, String port) throws Exception {
    socket = new Socket(ip, Integer.valueOf(port));
  }

  /**
   * tear down connection after send message
   * @throws Exception
   */
  public void tearDownConn() throws Exception {
    socket.close();
  }

  /**
   * send message, for example AnchorMessage
   * @param msg
   * @throws Exception
   */
  public void sendAnchorMessage(AnchorMessage msg) throws Exception {
    ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
    oos.writeObject(msg);
    oos.close();
  }
}
